$(function () {
    //======================
    //======设置查询条件========
    //======================
    //内部公司
    $("#zjzy_toolbar [name='zjzy_gsId']").gsjgtree(1);
    //起始时间
    // $("#zjzy_toolbar [name='zjzy_endDate']").datebox();
    //搜索按钮
    $("#zjzy_toolbar [name='searchButton']").click(function () {
        $(grid).datagrid({
            queryParams:{
                enddt:$("#zjzy_toolbar [name='zjzy_endDate']").val(),
                gsid:$("#zjzy_toolbar [name='zjzy_gsId']").val(),
                type:$("#zjzy_toolbar [name='zjzy_lx']").val(),
                tc_flag:$("#zjzy_toolbar [name='zjzy_tc']").val()
            }});
    });


//======================
//========统计列表========
//======================
    var columns = [
        [
            {field:'jc', title:'公司名称', width:145, align:'center'},
            {field:'yufk', title:'预付款', width:145, formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.jc != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='bgyufk' gsid='{1}'>{2}</a>".stringFormat(color, r.gs_id, formatSl2(v, ",", "."));
                } else {
                    return formatSl2(v, ",", ".");
                }
            }, align:'right'},
            {field:'yingfk', title:'应付款', width:145, formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.jc != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='bgyingfk' gsid='{1}'>{2}</a>".stringFormat(color, r.gs_id, formatSl2(v, ",", "."));
                } else {
                    return formatSl2(v, ",", ".");
                }
            }, align:'right'},
            {field:'yingsk', title:'应收款', width:145, formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.jc != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='bgyingsk' gsid='{1}'>{2}</a>".stringFormat(color, r.gs_id, formatSl2(v, ",", "."));
                } else {
                    return formatSl2(v, ",", ".");
                }
            }, align:'right'},
            {field:'yusk', title:'预收款', width:145, formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.jc != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='bgyusk' gsid='{1}'>{2}</a>".stringFormat(color, r.gs_id, formatSl2(v, ",", "."));
                } else {
                    return formatSl2(v, ",", ".");
                }
            }, align:'right'},
            {field:'ztzj', title:'在途资金', width:145, formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.jc != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='bgztzj' gsid='{1}'>{2}</a>".stringFormat(color, r.gs_id, formatSl2(v, ",", "."));
                } else {
                    return formatSl2(v, ",", ".");
                }
            }, align:'right'},
            {field:'kcje', title:'库存金额', align:'right', formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.jc != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='bqkcje' gsid='{1}'>{2}</a>".stringFormat(color, r.gs_id, formatSl2(v, ",", "."));
                } else {
                    return formatSl2(v, ",", ".");
                }
            }, width:145},
            {field:'zjzy', title:'小计', align:'right', formatter:function (value) {
                var val = formatJe2(value, ",");
                return val;
            }, width:145}
        ]
    ];

    var grid = $('#zjzy_grid').datagrid({
        url:$.webPath() + '/tjbb/zjzy/zjzyList',
        toolbar:"#zjzy_toolbar",
        // pagination: true,
        rownumbers:true,
        singleSelect:true,
        collapsible:true,
        fit:true,
        fitColumns:true,
        pageNumber:1,
        showFooter:true,
        /*queryParams: {
         date: $("#zjzy_toolbar [name='zjzy_endDate']").val()
         },*/
        onLoadSuccess:function () {
            $(".bqkcje").unbind("click").bind("click", function () {
                showDetail('本期库存金额', $.webPath() + "/tjbb/zjzy/bqkcje", $(this).attr("gsid"));
            });
            $(".bgztzj").unbind("click").bind("click", function () {
                showDetail('本期在途资金', $.webPath() + "/tjbb/zjzy/bgztzj", $(this).attr("gsid"));
            });
            $(".bgyufk").unbind("click").bind("click", function () {
                //showDetail('本期预付款', $.webPath() + "/tjbb/zjzy/bgyufk", $(this).attr("gsid"));
                opentabs('供应商往来', "tjbb/gyswl/gyswl?gsid=" + $(this).attr('gsid') + "&enddt=" + $("#zjzy_endDate").datebox("getValue") + "&tc_flag=" + $("#zjzy_tc").combobox("getValue"), '');
                // opentabs('供应商往来', "tjbb/gyswl/gyswl",'');
            });
            $(".bgyingfk").unbind("click").bind("click", function () {
                //showDetail('本期应付款', $.webPath() + "/tjbb/zjzy/bgyingfk", $(this).attr("gsid"));
                opentabs('供应商往来', "tjbb/gyswl/gyswl?gsid=" + $(this).attr('gsid') + "&enddt=" + $("#zjzy_endDate").datebox("getValue") + "&tc_flag=" + $("#zjzy_tc").combobox("getValue"), '');
                //opentabs('供应商往来', "tjbb/gyswl/gyswl", '');
            });
            $(".bgyingsk").unbind("click").bind("click", function () {
                //showDetail('本期应收款', $.webPath() + "/tjbb/zjzy/bgyingsk", $(this).attr("gsid"));
                opentabs('客户往来', "tjbb/khwl/khwl?gsid=" + $(this).attr('gsid') + "&enddt=" + $("#zjzy_endDate").datebox("getValue") + "&tc_flag=" + $("#zjzy_tc").combobox("getValue"), '');
                //opentabs('客户往来', "tjbb/khwl/khwl", '');
            });
            $(".bgyusk").unbind("click").bind("click", function () {
                //showDetail('本期预收款', $.webPath() + "/tjbb/zjzy/bgyusk", $(this).attr("gsid"));
                opentabs('客户往来', "tjbb/khwl/khwl?gsid=" + $(this).attr('gsid') + "&enddt=" + $("#zjzy_endDate").datebox("getValue") + "&tc_flag=" + $("#zjzy_tc").combobox("getValue"), '');
                //opentabs('客户往来', "tjbb/khwl/khwl", '');
            });
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        columns:columns
    });

    //===========================================
//==============此页面用到的函数===================
//===========================================

    var showDetail = function (title, url, gsid) {
        var win = $.window({
            title:title,
            width:1050,
            height:470,
            cache:false,
            iconCls:'icon-tip',
            url:url + "?gsid=" + gsid + "&type=" + $("#zjzy_toolbar input[name='zjzy_lx']").val() + "&enddt=" + $("#zjzy_endDate").datebox("getValue") + "&tc_flag=" + $("#zjzy_toolbar input[name='zjzy_tc']").val(),
            modal:true,
            buttons:[]
        });
    }

//在右边center区域打开菜单，新增tab
    var opentabs = function (text, url, type) {
        if ($("#tabs").tabs('exists', text)) {
            $('#tabs').tabs('close', text);
            $('#tabs').tabs('add', {
                title:text,
                cache:true,
                closable:true,
                href:$.webPath() + '/' + url
            });
        } else if (type == 'bbtj') {
            $('#tabs').tabs('add', {
                title:text,
                cache:true,
                closable:true,
                content:'<iframe scrolling="no" frameborder="0" src="' + url + '" style="width:100%;height:100%;"></iframe>'
                //href: '../' + url
            });
        } else {
            $('#tabs').tabs('add', {
                title:text,
                cache:true,
                closable:true,
//                content: '<iframe scrolling="no" frameborder="0" src="../'+url+'" style="width:100%;height:100%;"></iframe>'
                href:$.webPath() + '/' + url
            });
        }
    }
});
